home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / emula / arosdv19.lha / AROS / exec / obtainsemaphore.c < prev    next >
C/C++ Source or Header  |  1996-10-24  |  3KB  |  109 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: obtainsemaphore.c,v 1.5 1996/10/24 15:50:52 aros Exp $
  4.     $Log: obtainsemaphore.c,v $
  5.     Revision 1.5  1996/10/24 15:50:52  aros
  6.     Use the official AROS macros over the __AROS versions.
  7.  
  8.     Revision 1.4  1996/08/13 13:56:04  digulla
  9.     Replaced AROS_LA by AROS_LHA
  10.     Replaced some AROS_LH*I by AROS_LH*
  11.     Sorted and added includes
  12.  
  13.     Revision 1.3  1996/08/01 17:41:14  digulla
  14.     Added standard header for all files
  15.  
  16.     Desc:
  17.     Lang: english
  18. */
  19. #include "exec_intern.h"
  20. #include "semaphores.h"
  21.  
  22. /*****************************************************************************
  23.  
  24.     NAME */
  25.     #include <exec/semaphores.h>
  26.     #include <clib/exec_protos.h>
  27.  
  28.     AROS_LH1(void, ObtainSemaphore,
  29.  
  30. /*  SYNOPSIS */
  31.     AROS_LHA(struct SignalSemaphore *, sigSem, A0),
  32.  
  33. /*  LOCATION */
  34.     struct ExecBase *, SysBase, 94, Exec)
  35.  
  36. /*  FUNCTION
  37.     Obtain an exclusive lock on a semaphore. If the semaphore is already
  38.     in use by another task this function will wait until the semaphore
  39.     becomes free.
  40.  
  41.     INPUTS
  42.     sigSem - Pointer to semaphore structure
  43.  
  44.     RESULT
  45.  
  46.     NOTES
  47.     This function preserves all registers.
  48.  
  49.     EXAMPLE
  50.  
  51.     BUGS
  52.  
  53.     SEE ALSO
  54.     ReleaseSemaphore()
  55.  
  56.     INTERNALS
  57.  
  58.     HISTORY
  59.     29-10-95    digulla automatically created from
  60.                 exec_lib.fd and clib/exec_protos.h
  61.     21-01-96    fleischer implementation
  62.  
  63. *****************************************************************************/
  64. {
  65.     AROS_LIBFUNC_INIT
  66.     AROS_LIBBASE_EXT_DECL(struct ExecBase *,SysBase)
  67.     struct Task *me;
  68.  
  69.     /* Get pointer to current task */
  70.     me=SysBase->ThisTask;
  71.  
  72.     /* Arbitrate for the semaphore structure */
  73.     Forbid();
  74.  
  75.     /* Check if the semaphore is in use by another task */
  76.     if(sigSem->ss_NestCount&&sigSem->ss_Owner!=me)
  77.     {
  78.     /*
  79.         I wish there was some shared memory available as a semaphore node.
  80.         Unfortunately it isn't - and I cannot rely on being able to get
  81.         some from AllocMem() at this point either, so I have to use a part
  82.         of the stack instead :-(.
  83.     */
  84.     struct SemaphoreNode sn;
  85.  
  86.     sn.node.ln_Pri =SN_TYPE_OBTAIN;
  87.     sn.node.ln_Name=(char *)SM_EXCLUSIVE;
  88.     sn.task        =me;
  89.  
  90.     /* Add the node to the semaphore's waiting queue. */
  91.     AddTail((struct List *)&sigSem->ss_WaitQueue,&sn.node);
  92.  
  93.     /* Wait until the semaphore is free */
  94.     Wait(SEMAPHORESIGF);
  95.  
  96.     /* ss_NestCount and ss_Owner are already set by ReleaseSemaphore() */
  97.     }else
  98.     {
  99.     /* The semaphore is free - take it over */
  100.     sigSem->ss_NestCount++;
  101.     sigSem->ss_Owner=me;
  102.     }
  103.  
  104.     /* All done */
  105.     Permit();
  106.     AROS_LIBFUNC_EXIT
  107. } /* ObtainSemaphore */
  108.  
  109.